﻿using MultiLang;
using System;
using System.Linq;
using System.Windows.Forms;
using BLToolkit.Data;
using BLToolkit.Data.Linq;

namespace Rsdn.Janus
{
    /// <summary>
    /// Редактор новых сообщений.
    /// </summary>
    internal class NewMessageEditor : IOutboxItemEditor
    {
        private readonly IServiceProvider _serviceProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="NewMessageEditor"/> class.
        /// </summary>
        /// <param name="serviceProvider">The serviceProvider.</param>
        public NewMessageEditor(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        #region IOutboxItemEditor Members
        /// <summary>
        /// Allows the edit.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool AllowEdit(object item)
        {
            return true;
        }

        /// <summary>
        /// Allows the delete.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool AllowDelete(object item)
        {
            return true;
        }

        /// <summary>
        /// Edits the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Edit(object item)
        {
            MessageInfo msgInfo;
            int id = ((NewMessage)item).ID;
            using (var db = _serviceProvider.CreateDBContext())
            {
                IQueryable<DataModel.IOutboxMessage> msg = db.OutboxMessages(m => m.ID == id);
                IQueryable<MessageInfo> messageInfos = msg
                    .Select(
                        m =>
                        new MessageInfo(
                            m.ID,
                            m.ForumID,
                            m.ReplyToID,
                            m.Subject,
                            m.Body));

                if (messageInfos.Count() > 0)
                {
                    msgInfo = messageInfos.Single();

                    //msg.Set(_ => _.Hold, _ => true).Update();
                    MessageEditor.EditMessage(ServiceProvider, MessageFormMode.Edit, msgInfo);
                }
                else
                {
                    MessageBox.Show(
                        ml.ml_string(361, "Запрошенное сообщение на найдено в базе"), 
                        ml.ml_string(362, "Информация"),              //MLHIDE
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Information);
                }
            }

            //IOutboxManager outboxManager = _serviceProvider.GetRequiredService<IOutboxManager>();

            //outboxManager.NewMessages.Refresh();
        }

        /// <summary>
        /// Deletes the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Delete(object item)
        {
            ((NewMessage)item).Delete();
        }
        #endregion

        public IServiceProvider ServiceProvider
        {
            get { return _serviceProvider; }
        }

    }
}
